package com.example.myapplication.filter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class OrCriteria implements Criteria {
    private Criteria criteria;
    private Criteria otherCriteria;

    public OrCriteria(Criteria criteria, Criteria otherCriteria) {
        this.criteria = criteria;
        this.otherCriteria = otherCriteria;
    }

    @Override
    public List<Person> meetCriteria(List<Person> people) {
        if (null == criteria || null == otherCriteria) {
            throw new RuntimeException("any other of the two criteria can`t be null");
        }
        List<Person> listOne = criteria.meetCriteria(people);
        List<Person> listTwo = otherCriteria.meetCriteria(people);
        Set<Person> set = new HashSet<>();
        set.addAll(listOne);
        set.addAll(listTwo);

        ArrayList<Person> list = new ArrayList<>();
        list.addAll(set);
        return list;
    }
}
